/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.sample;

import java.sql.PreparedStatement;
import org.json.JSONObject;
import org.webos.java.base.Frame;
import org.webos.java.base.FrameType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.root.Log;
import org.webos.java.base.NetworkManager;

/**
 *
 * @author kostya
 */

@FrameType(code="main-frame", jsClass="sample.MainFrame")
public class MainFrame extends Frame{

    private AddressBookApplication app;

    public MainFrame(AddressBookApplication app, Workspace ws) {
        super(app, ws);
        this.app = app;
        setIcon("ab_icons ab_single");
        setTitle("Edit addressbook contact:");
    }

    @RemoteMessage
    public void newEntry(JSONObject data){
        app.treeFrame.newEntry();
    }

    @RemoteMessage
    public JSONObject removeEntry(JSONObject data){
        if(app.treeFrame.selected!=null && app.connection.isActive()){
            try {
                PreparedStatement st = app.connection.prepareQuery("delete from entries where id=?");
                st.setInt(1, app.treeFrame.selected.id);
                st.execute();
                app.treeFrame.entries.remove(app.treeFrame.selected);
                if(app.treeFrame.entries.size()>0)
                    app.treeFrame.selected = app.treeFrame.entries.get(0);
                else
                    app.treeFrame.selected = null;
                app.treeFrame.sendMessage("reloadTree", app.treeFrame.getEntries(null));
                return null;
            } catch (Throwable t) {
                Log.error(this, "Exception thrown: " + t.getMessage());
                t.printStackTrace();
            }
        }
        return NetworkManager.getErrorObject(102, "Can't remove addressbook entry");
    }

    @RemoteMessage
    public JSONObject saveEntry(JSONObject data){
        try {
            Log.debug(this, "Saving:", data.opt("lastName"), data.opt("firstName"),
                    data.opt("email"), data.opt("notes"));
            if(data.optString("lastName", "").equals("")
                    || data.optString("firstName", "").equals("")){
                return NetworkManager.getErrorObject(100, "Names (first and last) are required");
            }
            if(app.treeFrame.selected!=null){
                if(app.connection.isActive()){
                    PreparedStatement st = app.connection.getConnection().prepareStatement("update entries set lastname=?, " +
                            "firstname=?, email=?, notes=?, work_email=?, " +
                            "work_phone=?, home_phone=?, cell_phone=?, " +
                            "birthday=? where id=?");
                    st.setString(1, data.optString("lastName", ""));
                    st.setString(2, data.optString("firstName", ""));
                    st.setString(3, data.optString("email", ""));
                    st.setString(4, data.optString("notes", ""));
                    st.setString(5, data.optString("work_email", ""));
                    st.setString(6, data.optString("work_phone", ""));
                    st.setString(7, data.optString("home_phone", ""));
                    st.setString(8, data.optString("cell_phone", ""));
                    st.setString(9, data.optString("birthday", ""));
                    st.setInt(10, app.treeFrame.selected.id);
                    st.execute();
                    st.close();
                }
                app.treeFrame.selected.firstName = data.optString("firstName", "");
                app.treeFrame.selected.lastName = data.optString("lastName", "");
                app.treeFrame.selected.notes = data.optString("notes", "");
                app.treeFrame.selected.email = data.optString("email", "");
                app.treeFrame.selected.work_email = data.optString("work_email", "");
                app.treeFrame.selected.work_phone = data.optString("work_phone", "");
                app.treeFrame.selected.home_phone = data.optString("home_phone", "");
                app.treeFrame.selected.cell_phone = data.optString("cell_phone", "");
                app.treeFrame.selected.birthday = data.optString("birthday", "");

                app.treeFrame.sendMessage("reloadTree", app.treeFrame.getEntries(null));
                return null;
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(101, "Can't save address book entry. Database error");
    }

}
